﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ExFlash.Tags;
using Be.Windows.Forms;
using Microsoft.Win32;

namespace ExFlash
{
    public enum TagFilters
    {
        All = 0,
        Shapes = 1
    }

    public partial class Form1 : Form
    {
        FlashFile ff;
        Boolean FileLoaded = false;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (FileLoaded)
            { 
                return; 
            }
            
            if (!File.Exists(textSwfPath.Text))
            {
                MessageBox.Show("File does not exist");
                return;
            }

            FileStream fs = File.Open(textSwfPath.Text, FileMode.Open,FileAccess.Read);

            SavePathToRegistry(textSwfPath.Text);

            ff = new FlashFile();
            ff.Load(fs);
            hexBox1.ByteProvider = ff.ByteProvider;
            hexBox1.Select(20, 10);

            treeViewTags.BeginUpdate();

            LoadTags(ff.Tags);

            treeViewTags.EndUpdate();

            shapeViewer1.FrameSize = ff.Header.FrameSize;

            FileLoaded = true;
        }

        private void LoadTags(IEnumerable<Tag> tags)
        {
            switch ((TagFilters)comboBoxfilter.SelectedValue)
            {
                case TagFilters.All:
                    break;
                case TagFilters.Shapes:
                    {
                        tags = tags.Where(x => x is DefineShapeTag);
                        break;
                    }
                default:
                    break;
            }

            treeViewTags.BeginUpdate();

            treeViewTags.Nodes.Clear();

            LoadTags(treeViewTags.Nodes, tags);

            treeViewTags.EndUpdate();
        }

        private void LoadTags(TreeNodeCollection nodes, IEnumerable<Tag> tags)
        {
            foreach (Tag tag in tags)
            {
                String temp = tag.ToString();
                TreeNode node = nodes.Add(tag.ToString() + ", Tag Length: " + tag.TagLength);
                node.Tag = tag;

                DefineSpriteTag SpriteTag;

                if ((SpriteTag = tag as DefineSpriteTag) != null)
                {
                    LoadTags(node.Nodes, SpriteTag.ControlTags);
                }
            }
        }

        private void treeViewTags_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            Tag tag = (Tag)e.Node.Tag;
            propertyGrid1.SelectedObject = tag;
            hexBox1.Select(tag.TagOffset, tag.TagHeaderLength + tag.TagLength);

            shapeViewer1.ShapeTag = tag as DefineShapeTag;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                RegistryKey SoftwareKey = Registry.CurrentUser.OpenSubKey("Software", true);
                
                RegistryKey key = SoftwareKey.OpenSubKey("ExFlash", true);

                if (key == null)
                {
                    key = SoftwareKey.CreateSubKey("ExFlash");
                }

                object SwfPath = key.GetValue("SwfDefaultPath");

                if (SwfPath == null)
                {
                    SwfPath = @"C:\Development\exflash\0.swf";
                    key.SetValue("SwfDefaultPath", SwfPath, RegistryValueKind.String);
                }

                textSwfPath.Text = SwfPath.ToString();
            }
            catch (System.Exception)
            {
                
            }

            comboBoxfilter.DataSource = Enum.GetValues(typeof(TagFilters));
        }

        private void SavePathToRegistry(String path)
        {
            Registry.CurrentUser.OpenSubKey("Software", true).OpenSubKey("ExFlash", true).SetValue("SwfDefaultPath", path);
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            if (!FileLoaded)
                return;
            
            txtSearch.Text = String.Empty;

            LoadTags(ff.Tags);
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            if (!FileLoaded)
                return;

            LoadTags(ff.Tags.Where(x => x.ToString().ToLower().Contains(txtSearch.Text.ToLower())));        
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                buttonSearch_Click(sender, e);
            }
        }

        private void comboBoxfilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!FileLoaded)
                return;

            txtSearch.Text = String.Empty;

            LoadTags(ff.Tags);
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            
            try
            {
                String dir = Path.GetDirectoryName(textSwfPath.Text);

                if (Directory.Exists(dir))
                {
                    dialog.InitialDirectory = dir;
                }
            }
            catch (Exception)
            {
            }

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                textSwfPath.Text = dialog.FileName;
            }

            dialog.Dispose();
        }

    }
}
